classdef gi < handle
  properties (Constant)
    initialized = glib.gi.doInitialization()
  end

  properties (Constant, Access=protected)
    theRepos = glib.gi()
  end

  properties (Access=protected)
    pkgs = containers.Map
  end

  methods (Static)
    function ret = repos()
      ret = glib.gi.theRepos;
    end

    function ret = getPath()
      ret = glib.g_irepository_matlab_get_search_path();
    end

    function addPath(dir)
      glib.g_irepository_matlab_prepend_search_path(dir);
    end

    function ret = doInitialization()
      glib.g_matlab_init();
      %glib.g_irepository_matlab_prepend_search_path('@INTROSPECTION_TYPELIBDIR@');
      ret = true;
    end

  end  % Static Methods

  methods
    function varargout = subsref(A, S)
      if ~strcmp(S(1).type, '.'), error 'Unsupported first subscript to gi', end
      pkgname = S(1).subs;
      if ~A.pkgs.isKey(pkgname)		% Attempt to load package
        glib.g_irepository_matlab_load_pkg(pkgname);
        A.pkgs(pkgname) = glib.GIPackage(pkgname);
      end
      pkg = A.pkgs(pkgname);
      if length(S) == 1
        if nargout > 1, error 'Too many outputs for package', end
        varargout{1} = pkg;
      else
        varargout = subsref(pkg, S(2:length(S)), nargout);
      end % only 1 subscript?
    end  % subsref

    function delete(obj)
      disp('In gi.dlete')
    end

    function s = char(obj)
      s = 'GIRepository';
    end

    function disp(obj)
      disp(char(obj));
    end
  end  % Public methods

  methods (Access=private)
    function obj = gi()
    end
  end

end  % Classdef
